"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : GS_GLOBAL_CONFIG
Case Name   : drop global configuration方式删除参数weak_password,合理报错
Create At   : 2022/07/22
Owner       : opentestcase004
Description :
    1.向系统表gs_global_config添加一个弱口令
    2.查询系统表gs_global_config
    3.drop global configuration方式删除参数weak_password
    4.清理环境
Expect      :
    1.成功
    2.返回含新增参数
    3.合理报错
    4.成功
History     :
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Confirguration(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')

    def test_alter_global_configuration(self):
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        step = 'step1:向系统表gs_global_config添加一个弱口令 expect:成功'
        self.logger.info(step)
        create_weak = self.primary_sh.execut_db_sql(f'''
             create weak password dictionary with values ('Test@123');''')
        self.logger.info(create_weak)
        self.assertIn('CREATE WEAK PASSWORD DICTIONARY', create_weak,
                      '执行失败' + step)

        step = 'step2:查询系统表gs_global_config expect:返回含新增参数'
        self.logger.info(step)
        select_configuration = self.primary_sh.execut_db_sql(
            f'''select * from gs_global_config;''')
        self.logger.info(select_configuration)
        self.assertIn('weak_password | Test@123',
                      select_configuration, '执行失败' + step)

        step = 'step3:drop global configuration方式删除参数weak_password ' \
               'expect:合理报错'
        self.logger.info(step)
        drop_configuration = self.primary_sh.execut_db_sql(
            f'''drop global configuration weak_password;''')
        self.logger.info(drop_configuration)
        self.assertIn("ERROR:  Don't drop config named 'weak_password'",
                      drop_configuration, '执行失败' + step)

    def tearDown(self):
        step = 'step4:清理环境 expect:成功'
        self.logger.info(step)
        clean_environment = self.primary_sh.execut_db_sql(
            f'''drop weak password dictionary;''')
        self.logger.info(clean_environment)
        self.assertIn('DROP WEAK PASSWORD DICTIONARY', clean_environment,
                      '执行失败' + step)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
